package com.ywqf_api.service.impl;

import com.ywqf_api.base.BaseException;
import com.ywqf_api.dao.MobileDao;
import com.ywqf_api.dao.WechatPaymentDao;
import com.ywqf_api.dto.in.OrderParDto;
import com.ywqf_api.entity.order;
import com.ywqf_api.service.WechatPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class WechatPaymentServiceImpl implements WechatPaymentService {

    @Autowired
    private MobileDao mobileDao;

    @Autowired
    private WechatPaymentDao wechatPaymentDao;

    /**
     * 插入订单
     * @param orderParDto
     * @return
     */
    @Override
    public String insrtPaymentBill(OrderParDto orderParDto) {
        int type = orderParDto.getType();
        BigDecimal price = orderParDto.getPrice();
        if(type == 1){//水费
            int identification = 1;
            order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
            Date day = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
            String orderNum;
            if (good == null) {
                orderNum = "W" + longstr2 + "00001";
            } else {
                long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
                if (aa == longstr2) {
                    long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                    bb = bb + 1;
                    String billNumber = String.valueOf(bb);
                    String[] ss = {"00000", "0000", "000", "00", "0"};
                    billNumber = ss[billNumber.length()] + billNumber;
                    orderNum = "W" + longstr2 + billNumber;
                } else {
                    orderNum = "W" + longstr2 + "00001";
                }
            }
            orderParDto.setOrderNum(orderNum);
            orderParDto.setWaterRate(price);
            int order = wechatPaymentDao.saveWaterOrder(orderParDto);
            if(order > 0){
                return orderNum;
            }else{
                throw new BaseException("添加失败");
            }

        }

        if(type == 2){//电费
            int identification = 2;
            order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
            Date day = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
            String orderNum;
            if (good == null) {
                orderNum = "E" + longstr2 + "00001";
            } else {
                long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
                if (aa == longstr2) {
                    long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                    bb = bb + 1;
                    String billNumber = String.valueOf(bb);
                    String[] ss = {"00000", "0000", "000", "00", "0"};
                    billNumber = ss[billNumber.length()] + billNumber;
                    orderNum = "E" + longstr2 + billNumber;
                } else {
                    orderNum = "E" + longstr2 + "00001";
                }
            }
            orderParDto.setOrderNum(orderNum);
            orderParDto.setElectricityRate(price);
            int order = wechatPaymentDao.saveElectricityOrder(orderParDto);
            if(order > 0){
                return orderNum;
            }else{
                throw new BaseException("添加失败");
            }

        }

        if(type == 3){//采暖费
            int identification = 3;
            order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
            Date day = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
            String orderNum;
            if (good == null) {
                orderNum = "HW" + longstr2 + "00001";
            } else {
                String num = good.getOrderNum();
                long aa = Long.valueOf(good.getOrderNum().substring(2, 8));
                if (aa == longstr2) {
                    long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                    bb = bb + 1;
                    String billNumber = String.valueOf(bb);
                    String[] ss = {"00000", "0000", "000", "00", "0"};
                    billNumber = ss[billNumber.length() - 1] + billNumber;
                    orderNum = "HW" + longstr2 + billNumber;
                } else {
                    orderNum = "HW" + longstr2 + "00001";
                }
            }
            orderParDto.setOrderNum(orderNum);
            orderParDto.setPaymentAmount(price);
            int order = wechatPaymentDao.saveHeatingOrder(orderParDto);
            if(order > 0){
                return orderNum;
            }else{
                throw new BaseException("添加失败");
            }

        }

        if(type == 4){//车位费
            int identification = 4;
            order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
            Date day = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
            String orderNum;
            if (good == null) {
                orderNum = "P" + longstr2 + "00001";
            } else {
                long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
                if (aa == longstr2) {
                    long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                    bb = bb + 1;
                    String billNumber = String.valueOf(bb);
                    String[] ss = {"00000", "0000", "000", "00", "0"};
                    billNumber = ss[billNumber.length()] + billNumber;
                    orderNum = "P" + longstr2 + billNumber;
                } else {
                    orderNum = "P" + longstr2 + "00001";
                }
            }
            orderParDto.setOrderNum(orderNum);
            orderParDto.setPaymentAmount(price);
            int order = wechatPaymentDao.saveParkingOrder(orderParDto);
            if(order > 0){
                return orderNum;
            }else{
                throw new BaseException("添加失败");
            }

        }

        if(type == 5){//物业费
            int identification = 5;
            order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
            Date day = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
            String orderNum;
            if (good == null) {
                orderNum = "H" + longstr2 + "00001";
            } else {
                long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
                if (aa == longstr2) {
                    long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                    bb = bb + 1;
                    String billNumber = String.valueOf(bb);
                    String[] ss = {"00000", "0000", "000", "00", "0"};
                    billNumber = ss[billNumber.length()] + billNumber;
                    orderNum = "H" + longstr2 + billNumber;
                } else {
                    orderNum = "H" + longstr2 + "00001";
                }
            }
            orderParDto.setOrderNum(orderNum);
            orderParDto.setPaymentAmount(price);
            int order = wechatPaymentDao.saveEstateOrder(orderParDto);
            if(order > 0){
                return orderNum;
            }else{
                throw new BaseException("添加失败");
            }

        }
        throw new BaseException("添加失败");
    }
}
